﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Kangaroo.Deferreds;
using Kangaroo.IDAL;
using Kangaroo.Model;

namespace Kangaroo.DAL.SqlServer
{
    public class Alarm : IAlarm
    {
        public AlarmInfo[] ListAlarmsAfter(long key)
        {
            var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString,
                CommandType.StoredProcedure, "ListAlarmsAfter",
                new SqlParameter("@key", key));
            var alarms = new List<AlarmInfo>();
            while(reader.Read())
            {
                alarms.Add(GetAlarmInfoFromReader(reader));
            }
            reader.Close();
            return alarms.ToArray();
        }

        public IDeferred<AlarmInfo[]> ListAlarmsAfterAsync(long key)
        {
            return SqlHelper.ExecuteReaderAsync(SqlHelper.ConnectionString,
                CommandType.StoredProcedure, "ListAlarmsAfter",
                new SqlParameter("@key", key)).Pipe(reader =>
                {
                    var alarms = new List<AlarmInfo>();
                    while(reader.Read())
                    {
                        alarms.Add(GetAlarmInfoFromReader(reader));
                    }
                    reader.Close();
                    return alarms.ToArray();
                }, General.DataErrorHandler);
        }

        public AlarmInfo[] ListLastAlarmForEachChannel()
        {
            var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString,
                CommandType.StoredProcedure, "ListLastAlarmForEachChannel");
            var alarms = new List<AlarmInfo>();
            while(reader.Read())
            {
                alarms.Add(GetAlarmInfoFromReader(reader));
            }
            reader.Close();
            return alarms.ToArray();
        }

        public IDeferred<AlarmInfo[]> ListLastAlarmForEachChannelAsync()
        {
            return SqlHelper.ExecuteReaderAsync(SqlHelper.ConnectionString,
                CommandType.StoredProcedure, "ListLastAlarmForEachChannel").Pipe(reader =>
                {
                    var alarms = new List<AlarmInfo>();
                    while(reader.Read())
                    {
                        alarms.Add(GetAlarmInfoFromReader(reader));
                    }
                    reader.Close();
                    return alarms.ToArray();
                }, General.DataErrorHandler);
        }

        private static AlarmInfo GetAlarmInfoFromReader(SqlDataReader reader)
        {
            return new AlarmInfo
            {
                Key = (long)reader["Key"],
                ChannelKey = (int)reader["ChannelKey"],
                Channel = new ChannelInfo(
                    (int)reader["ChannelKey"],
                    reader["DevId"] != DBNull.Value ? (string)reader["DevId"] : "未定义",
                    reader["DevChn"] != DBNull.Value ? (short)reader["DevChn"] : (short)(-1),
                    reader["EquipId"] != DBNull.Value ? (string)reader["EquipId"] : "未定义",
                    reader["EquipPhase"] != DBNull.Value ? (byte)reader["EquipPhase"] : (byte)255),
                AlarmTime = (DateTime)reader["AlarmTime"],
                AlarmType = (string)reader["AlarmType"],
                AlarmLevel = (byte)reader["AlarmLevel"],
                AlarmValue = (float)reader["AlarmValue"]
            };
        }
    }
}
